﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

public partial class Test : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		//string strId = "getygxx";
		//string strPassword = "100000";

		//ASCIIEncoding encoding = new ASCIIEncoding();
		//string postData = "func=" + strId;
		//postData += ("&usysid=" + strPassword);

		//byte[] data = encoding.GetBytes(postData);

		//// Prepare web request...
		//HttpWebRequest myRequest =
		// (HttpWebRequest)WebRequest.Create("http://172.16.8.80/websrv/ws_server.php");

		//myRequest.Method = "POST";
		//myRequest.ContentType = "application/x-www-form-urlencoded";
		//myRequest.ContentLength = data.Length;
		//Stream newStream = myRequest.GetRequestStream();

		//// Send the data.
		//newStream.Write(data, 0, data.Length);
		//newStream.Close();

		//// Get response
		//HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
		//StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
		//string content = reader.ReadToEnd();
		//Console.WriteLine(content);

		JavaScriptSerializer serializer = new JavaScriptSerializer();

		//string strTemp = GetUrltoHtml("http://172.16.8.80/websrv/ws_server.php?func=getygxx&usysid=100000", "application/x-www-form-urlencoded");

		System.Net.WebRequest wReq = System.Net.WebRequest.Create("http://172.16.8.80/websrv/ws_server.php?func=getygxx&usysid=100000");
		System.Net.WebResponse wResp = wReq.GetResponse();
		//Response.Write(serializer.Serialize("http://172.16.8.80/websrv/ws_server.php?func=getygxx&usysid=100000"));
		//Response.Write(serializer.Serialize(wResp.GetResponseStream()));

		//System.IO.Stream respStream = wResp.GetResponseStream();

		//Response.Write(GetHttp("http://172.16.8.80/websrv/ws_server.php?func=getygxx&usysid=100000", HttpContext.Current));
		DLL.Person.Entity clsPerson = new DLL.Person.Entity();



		clsPerson = (DLL.Person.Entity)serializer.Deserialize(GetHttp("http://172.16.8.80/websrv/ws_server.php?func=checkygxx&gh=020917&sfzh=13020419810321061x&xm=" + HttpUtility.UrlEncode("王增辉", System.Text.Encoding.Default), HttpContext.Current), clsPerson.GetType());
		//Response.Write(clsPerson.xm);
	 	//Response.Write(HttpUtility.UrlEncode(clsPerson.xm, System.Text.Encoding.Unicode));
	}

	public static string GetUrltoHtml(string Url, string type)
	{
		try
		{
			System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
			// Get the response instance.
			System.Net.WebResponse wResp = wReq.GetResponse();
			//return wResp.ToString();
			System.IO.Stream respStream = wResp.GetResponseStream();
			// Dim reader As StreamReader = New StreamReader(respStream)
			System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(wResp.Headers.ToString()));
			return reader.ReadToEnd();
		}
		catch (System.Exception ex)
		{  //errorMsg = ex.Message;
		}
		return "";
	}

	public static string GetHttp(string url, HttpContext httpContext)
	{
		string queryString = "?";

		foreach (string key in httpContext.Request.QueryString.AllKeys)
		{
			queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
		}

		queryString = queryString.Substring(0, queryString.Length - 1);

		//url = HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);

		HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

		httpWebRequest.ContentType = "application/json";
		httpWebRequest.Method = "GET";
		httpWebRequest.Timeout = 20000;

		//byte[] btBodys = Encoding.UTF8.GetBytes(body);
		//httpWebRequest.ContentLength = btBodys.Length;
		//httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

		HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

		StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.Default);
		
		string responseContent = streamReader.ReadToEnd();

		httpWebResponse.Close();
		streamReader.Close();

		return responseContent;
	}


}